const { ref } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage } = ElementPlus

export function useSurpassbill() {
  let surpassbillDialogVisible = ref(false);//超越单查看对话框显示
  let surpassbillInfoList = ref([]);//超越单信息表格数据
  let surpassDocList = ref([]);//超越单附件信息表格数据

  const getSurpassbillInfo = async configObj => {
    const { activeMenuName, unCheckDetailListRef, childContainerTableRef, containerInfo, currentSelectContainer, baseAPI } = configObj;
    const unCheckSelectRows = unCheckDetailListRef.value.getSelectionRows();//【待检验信息】勾选的行
    const childContainerSelectRows = childContainerTableRef.value.getSelectionRows();//【子批次号】勾选的行
    let selectRows = activeMenuName == '待检验' ? unCheckSelectRows : childContainerSelectRows;
    const { containertype } = containerInfo;

    if (activeMenuName == '待检验' && unCheckSelectRows.length === 0) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }

    if (activeMenuName != '待检验' && !containerInfo.containerstepdispatchid) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }

    if (containertype === 7 && selectRows.length === 0) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }

    let reportData = [];
    if (activeMenuName != '待检验' && containertype !== 7) {
      reportData.push({ ...currentSelectContainer });
    } else {
      for (const i of selectRows) {
        reportData.push({ ...currentSelectContainer, containerid: i.containerid });
      }
    }

    const { data, status: { code, msg } } = await useFetch({
      url: `${baseAPI}?actions=getSurpassbillInfoList`,
      data: { type: 'getSurpassbillInfoList', reportData }
    });

    if (!data.value) {
      return ElMessage({ type: 'error', message: msg, grouping: true });
    } else {
      surpassbillInfoList.value = data.value;//超越单信息
    }

    surpassbillDialogVisible.value = true;// 打开超越单查看对话框

    const { data: docList } = await useFetch({
      url: `${baseAPI}?actions=getSurpassbillDocList`,
      data: { type: 'getSurpassbillDocList', postData: surpassbillInfoList.value }
    });
    surpassDocList.value = docList.value;//超越单附件信息

    for (const i of surpassDocList.value) {
      if (window.location.hostname === 'localhost') {
        i.url = `http://localhost/InSiteWebApplication/Temp/${i.accessoriespath}`
      } else {
        i.url = `http://10.15.7.76/InSiteWebApplication/Temp/${i.accessoriespath}`
      }
    }
  }

  return {
    surpassbillDialogVisible, surpassbillInfoList, surpassDocList, getSurpassbillInfo
  };
}